﻿using TTQT.TTQT_Code;
using TTQT.TTQT_Code.Info;

namespace TTQT.Administration
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for CmdManager.
	/// </summary>
	public partial class CmdManagement : MasterControl// System.Web.UI.UserControl
	{

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				cmdDelete.Attributes.Add("onclick", "delete_Confirm");
				refreshData();
			}
		}

		private void refreshData()
		{
			DataAccessObject.instance().FillToListBox(lstCmds.Items);
			DataAccessObject.instance().FillToListBox(cboParent.Items);
			cboParent.Items.Add(new ListItem("Root","0"));
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion

		protected void cmdUpdate_Click(object sender, System.EventArgs e)
		{
            CmdInfo info=new CmdInfo();
			info.Cmd_Id = TTQTCommon.ToInt32(this.txtID.Text, 0);
			info.Cmd_Name = this.txtName.Text;
			info.Cmd_Path = this.txtPath.Text;
			info.Cmd_Parent_Id = TTQTCommon.ToInt32(this.cboParent.SelectedValue, 0);
			if(this.chkDisplay.Checked) info.Display = 1;
			else info.Display = 0;
            info.OrderCode = txtOrderCode.Text;
			DataAccessObject.instance().updateCmd(info);
			//Log here
			refreshData();
		    try
		    {
		        cboParent.SelectedValue = ViewState["CmdID"].ToString();
		    }
		    catch (Exception)
		    {
		        
		    }
		}

		protected void lstCmds_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			CmdInfo info = DataAccessObject.instance().getCmd(TTQTCommon.ToInt32(lstCmds.SelectedValue, 0));
			if(info!=null)
			{
				this.txtID.Text = info.Cmd_Id.ToString();
				this.txtName.Text = info.Cmd_Name;
				this.txtPath.Text = info.Cmd_Path;
				if(info.Display>0) chkDisplay.Checked = true;
				else chkDisplay.Checked = false;
                txtOrderCode.Text = info.OrderCode;
				try
				{
					this.cboParent.SelectedValue = info.Cmd_Parent_Id.ToString();
				}catch(Exception)
				{
					this.cboParent.SelectedIndex = this.cboParent.Items.Count - 1;
				}
			}
            ViewState["CmdID"] = this.cboParent.SelectedValue;
		}

		protected void cmdAddNew_Click(object sender, System.EventArgs e)
		{
			CmdInfo info=new CmdInfo();
			info.Cmd_Id = TTQTCommon.ToInt32(this.txtID.Text, 0);
			info.Cmd_Name = this.txtName.Text;
			info.Cmd_Path = this.txtPath.Text;
			info.Cmd_Parent_Id = TTQTCommon.ToInt32(this.cboParent.SelectedValue, 0);
			if(this.chkDisplay.Checked) info.Display = 1;
			else info.Display = 0;
            info.OrderCode = txtOrderCode.Text;
			if(DataAccessObject.instance().addCmd(info)<0)
			{
				//Không insert được;
			}
			refreshData();
		}

		protected void cmdDelete_Click(object sender, System.EventArgs e)
		{
			DataAccessObject.instance().deleteCmd(TTQTCommon.ToInt32(lstCmds.SelectedValue,-1));

			refreshData();
		}

		protected void cmdEmpty_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(navigateUrl(0));
		    this.txtID.Text = "";
			this.txtName.Text = "";
			this.txtPath.Text = "";
			try
			{
				this.cboParent.SelectedValue = "0";
			}
			catch(Exception)
			{
			}
		}


	}
}
